//
//  ChurenhuiViewController.m
//  ThePrivateKitchen
//
//  Created by lirui on 16/1/20.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "ChurenhuiViewController.h"

@interface ChurenhuiViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic, retain)NSMutableArray *dataArr; //接收解析数的数组
@property(nonatomic ,retain)UICollectionView *collectionView;
@property(nonatomic, assign)BOOL isRefrsh;
@end
static  NSInteger i = 0;
static NSInteger j = 0;
@implementation ChurenhuiViewController

- (void)dealloc
{
    [_dataArr release];
    [_collectionView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [ UIColor whiteColor];
    [self AFNetwork];
    [self creatcollectionview];
    [self loadMBProgress];
    self.navigationItem.title = @"厨友汇";
    self.dataArr = [ NSMutableArray array];
    [self.collectionView.mj_header beginRefreshing];
    
}
//数据请求
-(void)AFNetwork
{
    NSString *str  = [NSString stringWithFormat:@"http://api.izhangchu.com/?methodName=ShequList&page=%ld&size=%ld&token=0&user_id=0&version=4.1",i,j];
    [LRAFNetwork postUrlString:str body:nil response:JSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if (_isRefrsh) {
            [_dataArr removeAllObjects];
        }
        NSArray *arr = responseObject[@"data"][@"data"];
        [self.dataArr addObjectsFromArray:[ChurenhuiModel modelHanderWithArray:arr]];
        
        [_collectionView reloadData];
        [self.hud removeFromSuperview];
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}
#pragma mark -创建滚动视图
-(void)creatcollectionview
{
    UICollectionViewFlowLayout  *layout = [[ UICollectionViewFlowLayout alloc] init];
    //布局设置
    //滚动方向
    layout.scrollDirection =  UICollectionViewScrollDirectionVertical;
    //cell大小
    layout.itemSize = CGSizeMake(( WIDTH*375-10)/2 , HEIGHT*249);
    //行列间距
    //行
    layout.minimumLineSpacing = 10 *WIDTH;
    //列
    layout.minimumInteritemSpacing = 1*HEIGHT;
    //边界
    layout.sectionInset = UIEdgeInsetsMake(0,0,0,0);
    //头部
    //layout.headerReferenceSize = CGSizeMake(100, 1000);
    self.collectionView =[[ UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH *375, HEIGHT *667)    collectionViewLayout:layout];
    self.collectionView .delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [self.view addSubview:self.collectionView];
    //[_collectionview setContentOffset:CGPointMake(375 * 2, 0)];
    _collectionView.pagingEnabled = NO;
    
    [_collectionView registerClass:[ChufangdarenCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        //赋初值
        i = 1;
        self.isRefrsh = YES;
        
        [self AFNetwork];
    }];
    self.collectionView.mj_header = header;
    //
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        i++;
        j += 10;
        self.isRefrsh = NO;
        [self AFNetwork];
    }];
    self.collectionView.mj_footer = footer;
    


    
}
#pragma mark -必须实现的协议方法
//cell个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArr.count;
}
//cell设置
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ChufangdarenCollectionViewCell *cell = [ collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    ChurenhuiModel *m = _dataArr[ indexPath.item];
    cell.nick.text =m.nick;
    [cell.img sd_setImageWithURL:[NSURL URLWithString:m.image]];
    [cell.headImage sd_setImageWithURL:[NSURL URLWithString:m.head_img]];
    cell.time.text =m.create_time;
    
    
    return cell;
}
//点击方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    Chu_detailViewController *chuDetail =[[ Chu_detailViewController alloc] init];
    chuDetail.model = _dataArr[indexPath.row];
    [self.navigationController pushViewController:chuDetail animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
